<h1 id="对话框">对话框</h1>
<p>对话框是一个现代GUI应用不可或缺的一部分。对话是两个人之间的交流，对话框就是人与电脑之间的对话。对话框用来输入数据，修改数据，修改应用设置等等。</p>
<h2 id="输入文字">输入文字</h2>
<p><code>QInputDialog</code>提供了一个简单方便的对话框，可以输入字符串，数字或列表。</p>
<div class="sourceCode" id="cb1"><pre><code class="language-python"><a class="sourceLine" id="cb1-1" data-line-number="1"><span class="co">#!/usr/bin/python3</span></a>
<a class="sourceLine" id="cb1-2" data-line-number="2"><span class="co"># -*- coding: utf-8 -*-</span></a>
<a class="sourceLine" id="cb1-3" data-line-number="3"></a>
<a class="sourceLine" id="cb1-4" data-line-number="4"><span class="co">&quot;&quot;&quot;</span></a>
<a class="sourceLine" id="cb1-5" data-line-number="5"><span class="co">ZetCode PyQt5 tutorial </span></a>
<a class="sourceLine" id="cb1-6" data-line-number="6"></a>
<a class="sourceLine" id="cb1-7" data-line-number="7"><span class="co">In this example, we receive data from</span></a>
<a class="sourceLine" id="cb1-8" data-line-number="8"><span class="co">a QInputDialog dialog. </span></a>
<a class="sourceLine" id="cb1-9" data-line-number="9"></a>
<a class="sourceLine" id="cb1-10" data-line-number="10"><span class="co">Aauthor: Jan Bodnar</span></a>
<a class="sourceLine" id="cb1-11" data-line-number="11"><span class="co">Website: zetcode.com </span></a>
<a class="sourceLine" id="cb1-12" data-line-number="12"><span class="co">Last edited: August 2017</span></a>
<a class="sourceLine" id="cb1-13" data-line-number="13"><span class="co">&quot;&quot;&quot;</span></a>
<a class="sourceLine" id="cb1-14" data-line-number="14"></a>
<a class="sourceLine" id="cb1-15" data-line-number="15"><span class="im">from</span> PyQt5.QtWidgets <span class="im">import</span> (QWidget, QPushButton, QLineEdit, </a>
<a class="sourceLine" id="cb1-16" data-line-number="16">    QInputDialog, QApplication)</a>
<a class="sourceLine" id="cb1-17" data-line-number="17"><span class="im">import</span> sys</a>
<a class="sourceLine" id="cb1-18" data-line-number="18"></a>
<a class="sourceLine" id="cb1-19" data-line-number="19"><span class="kw">class</span> Example(QWidget):</a>
<a class="sourceLine" id="cb1-20" data-line-number="20"></a>
<a class="sourceLine" id="cb1-21" data-line-number="21">    <span class="kw">def</span> <span class="fu">__init__</span>(<span class="va">self</span>):</a>
<a class="sourceLine" id="cb1-22" data-line-number="22">        <span class="bu">super</span>().<span class="fu">__init__</span>()</a>
<a class="sourceLine" id="cb1-23" data-line-number="23">        </a>
<a class="sourceLine" id="cb1-24" data-line-number="24">        <span class="va">self</span>.initUI()</a>
<a class="sourceLine" id="cb1-25" data-line-number="25">        </a>
<a class="sourceLine" id="cb1-26" data-line-number="26">        </a>
<a class="sourceLine" id="cb1-27" data-line-number="27">    <span class="kw">def</span> initUI(<span class="va">self</span>):</a>
<a class="sourceLine" id="cb1-28" data-line-number="28"></a>
<a class="sourceLine" id="cb1-29" data-line-number="29">        <span class="va">self</span>.btn <span class="op">=</span> QPushButton(<span class="st">&#39;Dialog&#39;</span>, <span class="va">self</span>)</a>
<a class="sourceLine" id="cb1-30" data-line-number="30">        <span class="va">self</span>.btn.move(<span class="dv">20</span>, <span class="dv">20</span>)</a>
<a class="sourceLine" id="cb1-31" data-line-number="31">        <span class="va">self</span>.btn.clicked.<span class="ex">connect</span>(<span class="va">self</span>.showDialog)</a>
<a class="sourceLine" id="cb1-32" data-line-number="32">        </a>
<a class="sourceLine" id="cb1-33" data-line-number="33">        <span class="va">self</span>.le <span class="op">=</span> QLineEdit(<span class="va">self</span>)</a>
<a class="sourceLine" id="cb1-34" data-line-number="34">        <span class="va">self</span>.le.move(<span class="dv">130</span>, <span class="dv">22</span>)</a>
<a class="sourceLine" id="cb1-35" data-line-number="35">        </a>
<a class="sourceLine" id="cb1-36" data-line-number="36">        <span class="va">self</span>.setGeometry(<span class="dv">300</span>, <span class="dv">300</span>, <span class="dv">290</span>, <span class="dv">150</span>)</a>
<a class="sourceLine" id="cb1-37" data-line-number="37">        <span class="va">self</span>.setWindowTitle(<span class="st">&#39;Input dialog&#39;</span>)</a>
<a class="sourceLine" id="cb1-38" data-line-number="38">        <span class="va">self</span>.show()</a>
<a class="sourceLine" id="cb1-39" data-line-number="39">        </a>
<a class="sourceLine" id="cb1-40" data-line-number="40">        </a>
<a class="sourceLine" id="cb1-41" data-line-number="41">    <span class="kw">def</span> showDialog(<span class="va">self</span>):</a>
<a class="sourceLine" id="cb1-42" data-line-number="42">        </a>
<a class="sourceLine" id="cb1-43" data-line-number="43">        text, ok <span class="op">=</span> QInputDialog.getText(<span class="va">self</span>, <span class="st">&#39;Input Dialog&#39;</span>, </a>
<a class="sourceLine" id="cb1-44" data-line-number="44">            <span class="st">&#39;Enter your name:&#39;</span>)</a>
<a class="sourceLine" id="cb1-45" data-line-number="45">        </a>
<a class="sourceLine" id="cb1-46" data-line-number="46">        <span class="cf">if</span> ok:</a>
<a class="sourceLine" id="cb1-47" data-line-number="47">            <span class="va">self</span>.le.setText(<span class="bu">str</span>(text))</a>
<a class="sourceLine" id="cb1-48" data-line-number="48">        </a>
<a class="sourceLine" id="cb1-49" data-line-number="49">        </a>
<a class="sourceLine" id="cb1-50" data-line-number="50"><span class="cf">if</span> <span class="va">__name__</span> <span class="op">==</span> <span class="st">&#39;__main__&#39;</span>:</a>
<a class="sourceLine" id="cb1-51" data-line-number="51">    </a>
<a class="sourceLine" id="cb1-52" data-line-number="52">    app <span class="op">=</span> QApplication(sys.argv)</a>
<a class="sourceLine" id="cb1-53" data-line-number="53">    ex <span class="op">=</span> Example()</a>
<a class="sourceLine" id="cb1-54" data-line-number="54">    sys.exit(app.exec_())</a></code></pre></div>
<p>这个示例有一个按钮和一个输入框，点击按钮显示对话框，输入的文本会显示在输入框里。</p>
<pre><code class="language-python">text, ok = QInputDialog.getText(self, &#39;Input Dialog&#39;,
    &#39;Enter your name:&#39;)</code></pre>
<p>这是显示一个输入框的代码。第一个参数是输入框的标题，第二个参数是输入框的占位符。对话框返回输入内容和一个布尔值，如果点击的是OK按钮，布尔值就返回True。</p>
<pre><code class="language-python">if ok:
    self.le.setText(str(text))</code></pre>
<p>把得到的字符串放到输入框里。</p>
<p>程序展示：</p>
<figure>
<img class="whitelist" src="docs/PyQt5/images/5-inputdialog.png" alt="input dialog" />
</figure>
<h2 id="选取颜色">选取颜色</h2>
<p>QColorDialog提供颜色的选择。</p>
<div class="sourceCode" id="cb4"><pre><code class="language-python"><a class="sourceLine" id="cb4-1" data-line-number="1"><span class="co">#!/usr/bin/python3</span></a>
<a class="sourceLine" id="cb4-2" data-line-number="2"><span class="co"># -*- coding: utf-8 -*-</span></a>
<a class="sourceLine" id="cb4-3" data-line-number="3"></a>
<a class="sourceLine" id="cb4-4" data-line-number="4"><span class="co">&quot;&quot;&quot;</span></a>
<a class="sourceLine" id="cb4-5" data-line-number="5"><span class="co">ZetCode PyQt5 tutorial </span></a>
<a class="sourceLine" id="cb4-6" data-line-number="6"></a>
<a class="sourceLine" id="cb4-7" data-line-number="7"><span class="co">In this example, we select a color value</span></a>
<a class="sourceLine" id="cb4-8" data-line-number="8"><span class="co">from the QColorDialog and change the background</span></a>
<a class="sourceLine" id="cb4-9" data-line-number="9"><span class="co">color of a QFrame widget. </span></a>
<a class="sourceLine" id="cb4-10" data-line-number="10"></a>
<a class="sourceLine" id="cb4-11" data-line-number="11"><span class="co">Author: Jan Bodnar</span></a>
<a class="sourceLine" id="cb4-12" data-line-number="12"><span class="co">Website: zetcode.com </span></a>
<a class="sourceLine" id="cb4-13" data-line-number="13"><span class="co">Last edited: August 2017</span></a>
<a class="sourceLine" id="cb4-14" data-line-number="14"><span class="co">&quot;&quot;&quot;</span></a>
<a class="sourceLine" id="cb4-15" data-line-number="15"></a>
<a class="sourceLine" id="cb4-16" data-line-number="16"><span class="im">from</span> PyQt5.QtWidgets <span class="im">import</span> (QWidget, QPushButton, QFrame, </a>
<a class="sourceLine" id="cb4-17" data-line-number="17">    QColorDialog, QApplication)</a>
<a class="sourceLine" id="cb4-18" data-line-number="18"><span class="im">from</span> PyQt5.QtGui <span class="im">import</span> QColor</a>
<a class="sourceLine" id="cb4-19" data-line-number="19"><span class="im">import</span> sys</a>
<a class="sourceLine" id="cb4-20" data-line-number="20"></a>
<a class="sourceLine" id="cb4-21" data-line-number="21"><span class="kw">class</span> Example(QWidget):</a>
<a class="sourceLine" id="cb4-22" data-line-number="22">    </a>
<a class="sourceLine" id="cb4-23" data-line-number="23">    <span class="kw">def</span> <span class="fu">__init__</span>(<span class="va">self</span>):</a>
<a class="sourceLine" id="cb4-24" data-line-number="24">        <span class="bu">super</span>().<span class="fu">__init__</span>()</a>
<a class="sourceLine" id="cb4-25" data-line-number="25">        </a>
<a class="sourceLine" id="cb4-26" data-line-number="26">        <span class="va">self</span>.initUI()</a>
<a class="sourceLine" id="cb4-27" data-line-number="27">        </a>
<a class="sourceLine" id="cb4-28" data-line-number="28">        </a>
<a class="sourceLine" id="cb4-29" data-line-number="29">    <span class="kw">def</span> initUI(<span class="va">self</span>):      </a>
<a class="sourceLine" id="cb4-30" data-line-number="30"></a>
<a class="sourceLine" id="cb4-31" data-line-number="31">        col <span class="op">=</span> QColor(<span class="dv">0</span>, <span class="dv">0</span>, <span class="dv">0</span>) </a>
<a class="sourceLine" id="cb4-32" data-line-number="32"></a>
<a class="sourceLine" id="cb4-33" data-line-number="33">        <span class="va">self</span>.btn <span class="op">=</span> QPushButton(<span class="st">&#39;Dialog&#39;</span>, <span class="va">self</span>)</a>
<a class="sourceLine" id="cb4-34" data-line-number="34">        <span class="va">self</span>.btn.move(<span class="dv">20</span>, <span class="dv">20</span>)</a>
<a class="sourceLine" id="cb4-35" data-line-number="35"></a>
<a class="sourceLine" id="cb4-36" data-line-number="36">        <span class="va">self</span>.btn.clicked.<span class="ex">connect</span>(<span class="va">self</span>.showDialog)</a>
<a class="sourceLine" id="cb4-37" data-line-number="37"></a>
<a class="sourceLine" id="cb4-38" data-line-number="38">        <span class="va">self</span>.frm <span class="op">=</span> QFrame(<span class="va">self</span>)</a>
<a class="sourceLine" id="cb4-39" data-line-number="39">        <span class="va">self</span>.frm.setStyleSheet(<span class="st">&quot;QWidget { background-color: </span><span class="sc">%s</span><span class="st"> }&quot;</span> </a>
<a class="sourceLine" id="cb4-40" data-line-number="40">            <span class="op">%</span> col.name())</a>
<a class="sourceLine" id="cb4-41" data-line-number="41">        <span class="va">self</span>.frm.setGeometry(<span class="dv">130</span>, <span class="dv">22</span>, <span class="dv">100</span>, <span class="dv">100</span>)            </a>
<a class="sourceLine" id="cb4-42" data-line-number="42">        </a>
<a class="sourceLine" id="cb4-43" data-line-number="43">        <span class="va">self</span>.setGeometry(<span class="dv">300</span>, <span class="dv">300</span>, <span class="dv">250</span>, <span class="dv">180</span>)</a>
<a class="sourceLine" id="cb4-44" data-line-number="44">        <span class="va">self</span>.setWindowTitle(<span class="st">&#39;Color dialog&#39;</span>)</a>
<a class="sourceLine" id="cb4-45" data-line-number="45">        <span class="va">self</span>.show()</a>
<a class="sourceLine" id="cb4-46" data-line-number="46">        </a>
<a class="sourceLine" id="cb4-47" data-line-number="47">        </a>
<a class="sourceLine" id="cb4-48" data-line-number="48">    <span class="kw">def</span> showDialog(<span class="va">self</span>):</a>
<a class="sourceLine" id="cb4-49" data-line-number="49">      </a>
<a class="sourceLine" id="cb4-50" data-line-number="50">        col <span class="op">=</span> QColorDialog.getColor()</a>
<a class="sourceLine" id="cb4-51" data-line-number="51"></a>
<a class="sourceLine" id="cb4-52" data-line-number="52">        <span class="cf">if</span> col.isValid():</a>
<a class="sourceLine" id="cb4-53" data-line-number="53">            <span class="va">self</span>.frm.setStyleSheet(<span class="st">&quot;QWidget { background-color: </span><span class="sc">%s</span><span class="st"> }&quot;</span></a>
<a class="sourceLine" id="cb4-54" data-line-number="54">                <span class="op">%</span> col.name())</a>
<a class="sourceLine" id="cb4-55" data-line-number="55">            </a>
<a class="sourceLine" id="cb4-56" data-line-number="56">        </a>
<a class="sourceLine" id="cb4-57" data-line-number="57"><span class="cf">if</span> <span class="va">__name__</span> <span class="op">==</span> <span class="st">&#39;__main__&#39;</span>:</a>
<a class="sourceLine" id="cb4-58" data-line-number="58">    </a>
<a class="sourceLine" id="cb4-59" data-line-number="59">    app <span class="op">=</span> QApplication(sys.argv)</a>
<a class="sourceLine" id="cb4-60" data-line-number="60">    ex <span class="op">=</span> Example()</a>
<a class="sourceLine" id="cb4-61" data-line-number="61">    sys.exit(app.exec_())</a></code></pre></div>
<p>例子里有一个按钮和一个<code>QFrame</code>，默认的背景颜色为黑色，我们可以使用<code>QColorDialog</code>改变背景颜色。</p>
<pre><code class="language-python">col = QColor(0, 0, 0)</code></pre>
<p>初始化<code>QtGui.QFrame</code>的背景颜色。</p>
<pre><code class="language-python">col = QColorDialog.getColor()</code></pre>
<p>弹出一个<code>QColorDialog</code>对话框。</p>
<pre><code class="language-python">if col.isValid():
    self.frm.setStyleSheet(&quot;QWidget { background-color: %s }&quot;
        % col.name())</code></pre>
<p>我们可以预览颜色，如果点击取消按钮，没有颜色值返回，如果颜色是我们想要的，就从取色框里选择这个颜色。</p>
<p>程序展示：</p>
<figure>
<img class="whitelist" src="docs/PyQt5/images/5-colordialog.png" alt="color dialog" />
</figure>
<h2 id="选择字体">选择字体</h2>
<p><code>QFontDialog</code>能做字体的选择。</p>
<div class="sourceCode" id="cb8"><pre><code class="language-python"><a class="sourceLine" id="cb8-1" data-line-number="1"><span class="co">#!/usr/bin/python3</span></a>
<a class="sourceLine" id="cb8-2" data-line-number="2"><span class="co"># -*- coding: utf-8 -*-</span></a>
<a class="sourceLine" id="cb8-3" data-line-number="3"></a>
<a class="sourceLine" id="cb8-4" data-line-number="4"><span class="co">&quot;&quot;&quot;</span></a>
<a class="sourceLine" id="cb8-5" data-line-number="5"><span class="co">ZetCode PyQt5 tutorial </span></a>
<a class="sourceLine" id="cb8-6" data-line-number="6"></a>
<a class="sourceLine" id="cb8-7" data-line-number="7"><span class="co">In this example, we select a font name</span></a>
<a class="sourceLine" id="cb8-8" data-line-number="8"><span class="co">and change the font of a label. </span></a>
<a class="sourceLine" id="cb8-9" data-line-number="9"></a>
<a class="sourceLine" id="cb8-10" data-line-number="10"><span class="co">Author: Jan Bodnar</span></a>
<a class="sourceLine" id="cb8-11" data-line-number="11"><span class="co">Website: zetcode.com </span></a>
<a class="sourceLine" id="cb8-12" data-line-number="12"><span class="co">Last edited: August 2017</span></a>
<a class="sourceLine" id="cb8-13" data-line-number="13"><span class="co">&quot;&quot;&quot;</span></a>
<a class="sourceLine" id="cb8-14" data-line-number="14"></a>
<a class="sourceLine" id="cb8-15" data-line-number="15"><span class="im">from</span> PyQt5.QtWidgets <span class="im">import</span> (QWidget, QVBoxLayout, QPushButton, </a>
<a class="sourceLine" id="cb8-16" data-line-number="16">    QSizePolicy, QLabel, QFontDialog, QApplication)</a>
<a class="sourceLine" id="cb8-17" data-line-number="17"><span class="im">import</span> sys</a>
<a class="sourceLine" id="cb8-18" data-line-number="18"></a>
<a class="sourceLine" id="cb8-19" data-line-number="19"><span class="kw">class</span> Example(QWidget):</a>
<a class="sourceLine" id="cb8-20" data-line-number="20">    </a>
<a class="sourceLine" id="cb8-21" data-line-number="21">    <span class="kw">def</span> <span class="fu">__init__</span>(<span class="va">self</span>):</a>
<a class="sourceLine" id="cb8-22" data-line-number="22">        <span class="bu">super</span>().<span class="fu">__init__</span>()</a>
<a class="sourceLine" id="cb8-23" data-line-number="23">        </a>
<a class="sourceLine" id="cb8-24" data-line-number="24">        <span class="va">self</span>.initUI()</a>
<a class="sourceLine" id="cb8-25" data-line-number="25">        </a>
<a class="sourceLine" id="cb8-26" data-line-number="26">        </a>
<a class="sourceLine" id="cb8-27" data-line-number="27">    <span class="kw">def</span> initUI(<span class="va">self</span>):      </a>
<a class="sourceLine" id="cb8-28" data-line-number="28"></a>
<a class="sourceLine" id="cb8-29" data-line-number="29">        vbox <span class="op">=</span> QVBoxLayout()</a>
<a class="sourceLine" id="cb8-30" data-line-number="30"></a>
<a class="sourceLine" id="cb8-31" data-line-number="31">        btn <span class="op">=</span> QPushButton(<span class="st">&#39;Dialog&#39;</span>, <span class="va">self</span>)</a>
<a class="sourceLine" id="cb8-32" data-line-number="32">        btn.setSizePolicy(QSizePolicy.Fixed,</a>
<a class="sourceLine" id="cb8-33" data-line-number="33">            QSizePolicy.Fixed)</a>
<a class="sourceLine" id="cb8-34" data-line-number="34">        </a>
<a class="sourceLine" id="cb8-35" data-line-number="35">        btn.move(<span class="dv">20</span>, <span class="dv">20</span>)</a>
<a class="sourceLine" id="cb8-36" data-line-number="36"></a>
<a class="sourceLine" id="cb8-37" data-line-number="37">        vbox.addWidget(btn)</a>
<a class="sourceLine" id="cb8-38" data-line-number="38"></a>
<a class="sourceLine" id="cb8-39" data-line-number="39">        btn.clicked.<span class="ex">connect</span>(<span class="va">self</span>.showDialog)</a>
<a class="sourceLine" id="cb8-40" data-line-number="40">        </a>
<a class="sourceLine" id="cb8-41" data-line-number="41">        <span class="va">self</span>.lbl <span class="op">=</span> QLabel(<span class="st">&#39;Knowledge only matters&#39;</span>, <span class="va">self</span>)</a>
<a class="sourceLine" id="cb8-42" data-line-number="42">        <span class="va">self</span>.lbl.move(<span class="dv">130</span>, <span class="dv">20</span>)</a>
<a class="sourceLine" id="cb8-43" data-line-number="43"></a>
<a class="sourceLine" id="cb8-44" data-line-number="44">        vbox.addWidget(<span class="va">self</span>.lbl)</a>
<a class="sourceLine" id="cb8-45" data-line-number="45">        <span class="va">self</span>.setLayout(vbox)          </a>
<a class="sourceLine" id="cb8-46" data-line-number="46">        </a>
<a class="sourceLine" id="cb8-47" data-line-number="47">        <span class="va">self</span>.setGeometry(<span class="dv">300</span>, <span class="dv">300</span>, <span class="dv">250</span>, <span class="dv">180</span>)</a>
<a class="sourceLine" id="cb8-48" data-line-number="48">        <span class="va">self</span>.setWindowTitle(<span class="st">&#39;Font dialog&#39;</span>)</a>
<a class="sourceLine" id="cb8-49" data-line-number="49">        <span class="va">self</span>.show()</a>
<a class="sourceLine" id="cb8-50" data-line-number="50">        </a>
<a class="sourceLine" id="cb8-51" data-line-number="51">        </a>
<a class="sourceLine" id="cb8-52" data-line-number="52">    <span class="kw">def</span> showDialog(<span class="va">self</span>):</a>
<a class="sourceLine" id="cb8-53" data-line-number="53"></a>
<a class="sourceLine" id="cb8-54" data-line-number="54">        font, ok <span class="op">=</span> QFontDialog.getFont()</a>
<a class="sourceLine" id="cb8-55" data-line-number="55">        <span class="cf">if</span> ok:</a>
<a class="sourceLine" id="cb8-56" data-line-number="56">            <span class="va">self</span>.lbl.setFont(font)</a>
<a class="sourceLine" id="cb8-57" data-line-number="57">        </a>
<a class="sourceLine" id="cb8-58" data-line-number="58">        </a>
<a class="sourceLine" id="cb8-59" data-line-number="59"><span class="cf">if</span> <span class="va">__name__</span> <span class="op">==</span> <span class="st">&#39;__main__&#39;</span>:</a>
<a class="sourceLine" id="cb8-60" data-line-number="60">    </a>
<a class="sourceLine" id="cb8-61" data-line-number="61">    app <span class="op">=</span> QApplication(sys.argv)</a>
<a class="sourceLine" id="cb8-62" data-line-number="62">    ex <span class="op">=</span> Example()</a>
<a class="sourceLine" id="cb8-63" data-line-number="63">    sys.exit(app.exec_())</a></code></pre></div>
<p>我们创建了一个有一个按钮和一个标签的<code>QFontDialog</code>的对话框，我们可以使用这个功能修改字体样式。</p>
<pre><code class="language-python">font, ok = QFontDialog.getFont()</code></pre>
<p>弹出一个字体选择对话框。<code>getFont()</code>方法返回一个字体名称和状态信息。状态信息有OK和其他两种。</p>
<pre><code class="language-python">if ok:
    self.label.setFont(font)</code></pre>
<p>如果点击OK，标签的字体就会随之更改。</p>
<p>程序展示：</p>
<figure>
<img class="whitelist" src="docs/PyQt5/images/5-fontdialog.png" alt="font dialog" />
</figure>
<h2 id="选择文件">选择文件</h2>
<p><code>QFileDialog</code>给用户提供文件或者文件夹选择的功能。能打开和保存文件。</p>
<div class="sourceCode" id="cb11"><pre><code class="language-python"><a class="sourceLine" id="cb11-1" data-line-number="1"><span class="co">#!/usr/bin/python3</span></a>
<a class="sourceLine" id="cb11-2" data-line-number="2"><span class="co"># -*- coding: utf-8 -*-</span></a>
<a class="sourceLine" id="cb11-3" data-line-number="3"></a>
<a class="sourceLine" id="cb11-4" data-line-number="4"><span class="co">&quot;&quot;&quot;</span></a>
<a class="sourceLine" id="cb11-5" data-line-number="5"><span class="co">ZetCode PyQt5 tutorial </span></a>
<a class="sourceLine" id="cb11-6" data-line-number="6"></a>
<a class="sourceLine" id="cb11-7" data-line-number="7"><span class="co">In this example, we select a file with a</span></a>
<a class="sourceLine" id="cb11-8" data-line-number="8"><span class="co">QFileDialog and display its contents</span></a>
<a class="sourceLine" id="cb11-9" data-line-number="9"><span class="co">in a QTextEdit.</span></a>
<a class="sourceLine" id="cb11-10" data-line-number="10"></a>
<a class="sourceLine" id="cb11-11" data-line-number="11"><span class="co">Author: Jan Bodnar</span></a>
<a class="sourceLine" id="cb11-12" data-line-number="12"><span class="co">Website: zetcode.com </span></a>
<a class="sourceLine" id="cb11-13" data-line-number="13"><span class="co">Last edited: August 2017</span></a>
<a class="sourceLine" id="cb11-14" data-line-number="14"><span class="co">&quot;&quot;&quot;</span></a>
<a class="sourceLine" id="cb11-15" data-line-number="15"></a>
<a class="sourceLine" id="cb11-16" data-line-number="16"><span class="im">from</span> PyQt5.QtWidgets <span class="im">import</span> (QMainWindow, QTextEdit, </a>
<a class="sourceLine" id="cb11-17" data-line-number="17">    QAction, QFileDialog, QApplication)</a>
<a class="sourceLine" id="cb11-18" data-line-number="18"><span class="im">from</span> PyQt5.QtGui <span class="im">import</span> QIcon</a>
<a class="sourceLine" id="cb11-19" data-line-number="19"><span class="im">import</span> sys</a>
<a class="sourceLine" id="cb11-20" data-line-number="20"></a>
<a class="sourceLine" id="cb11-21" data-line-number="21"><span class="kw">class</span> Example(QMainWindow):</a>
<a class="sourceLine" id="cb11-22" data-line-number="22">    </a>
<a class="sourceLine" id="cb11-23" data-line-number="23">    <span class="kw">def</span> <span class="fu">__init__</span>(<span class="va">self</span>):</a>
<a class="sourceLine" id="cb11-24" data-line-number="24">        <span class="bu">super</span>().<span class="fu">__init__</span>()</a>
<a class="sourceLine" id="cb11-25" data-line-number="25">        </a>
<a class="sourceLine" id="cb11-26" data-line-number="26">        <span class="va">self</span>.initUI()</a>
<a class="sourceLine" id="cb11-27" data-line-number="27">        </a>
<a class="sourceLine" id="cb11-28" data-line-number="28">        </a>
<a class="sourceLine" id="cb11-29" data-line-number="29">    <span class="kw">def</span> initUI(<span class="va">self</span>):      </a>
<a class="sourceLine" id="cb11-30" data-line-number="30"></a>
<a class="sourceLine" id="cb11-31" data-line-number="31">        <span class="va">self</span>.textEdit <span class="op">=</span> QTextEdit()</a>
<a class="sourceLine" id="cb11-32" data-line-number="32">        <span class="va">self</span>.setCentralWidget(<span class="va">self</span>.textEdit)</a>
<a class="sourceLine" id="cb11-33" data-line-number="33">        <span class="va">self</span>.statusBar()</a>
<a class="sourceLine" id="cb11-34" data-line-number="34"></a>
<a class="sourceLine" id="cb11-35" data-line-number="35">        openFile <span class="op">=</span> QAction(QIcon(<span class="st">&#39;open.png&#39;</span>), <span class="st">&#39;Open&#39;</span>, <span class="va">self</span>)</a>
<a class="sourceLine" id="cb11-36" data-line-number="36">        openFile.setShortcut(<span class="st">&#39;Ctrl+O&#39;</span>)</a>
<a class="sourceLine" id="cb11-37" data-line-number="37">        openFile.setStatusTip(<span class="st">&#39;Open new File&#39;</span>)</a>
<a class="sourceLine" id="cb11-38" data-line-number="38">        openFile.triggered.<span class="ex">connect</span>(<span class="va">self</span>.showDialog)</a>
<a class="sourceLine" id="cb11-39" data-line-number="39"></a>
<a class="sourceLine" id="cb11-40" data-line-number="40">        menubar <span class="op">=</span> <span class="va">self</span>.menuBar()</a>
<a class="sourceLine" id="cb11-41" data-line-number="41">        fileMenu <span class="op">=</span> menubar.addMenu(<span class="st">&#39;&amp;File&#39;</span>)</a>
<a class="sourceLine" id="cb11-42" data-line-number="42">        fileMenu.addAction(openFile)       </a>
<a class="sourceLine" id="cb11-43" data-line-number="43">        </a>
<a class="sourceLine" id="cb11-44" data-line-number="44">        <span class="va">self</span>.setGeometry(<span class="dv">300</span>, <span class="dv">300</span>, <span class="dv">350</span>, <span class="dv">300</span>)</a>
<a class="sourceLine" id="cb11-45" data-line-number="45">        <span class="va">self</span>.setWindowTitle(<span class="st">&#39;File dialog&#39;</span>)</a>
<a class="sourceLine" id="cb11-46" data-line-number="46">        <span class="va">self</span>.show()</a>
<a class="sourceLine" id="cb11-47" data-line-number="47">        </a>
<a class="sourceLine" id="cb11-48" data-line-number="48">        </a>
<a class="sourceLine" id="cb11-49" data-line-number="49">    <span class="kw">def</span> showDialog(<span class="va">self</span>):</a>
<a class="sourceLine" id="cb11-50" data-line-number="50"></a>
<a class="sourceLine" id="cb11-51" data-line-number="51">        fname <span class="op">=</span> QFileDialog.getOpenFileName(<span class="va">self</span>, <span class="st">&#39;Open file&#39;</span>, <span class="st">&#39;/home&#39;</span>)</a>
<a class="sourceLine" id="cb11-52" data-line-number="52"></a>
<a class="sourceLine" id="cb11-53" data-line-number="53">        <span class="cf">if</span> fname[<span class="dv">0</span>]:</a>
<a class="sourceLine" id="cb11-54" data-line-number="54">            f <span class="op">=</span> <span class="bu">open</span>(fname[<span class="dv">0</span>], <span class="st">&#39;r&#39;</span>)</a>
<a class="sourceLine" id="cb11-55" data-line-number="55"></a>
<a class="sourceLine" id="cb11-56" data-line-number="56">            <span class="cf">with</span> f:</a>
<a class="sourceLine" id="cb11-57" data-line-number="57">                data <span class="op">=</span> f.read()</a>
<a class="sourceLine" id="cb11-58" data-line-number="58">                <span class="va">self</span>.textEdit.setText(data)        </a>
<a class="sourceLine" id="cb11-59" data-line-number="59">        </a>
<a class="sourceLine" id="cb11-60" data-line-number="60"><span class="cf">if</span> <span class="va">__name__</span> <span class="op">==</span> <span class="st">&#39;__main__&#39;</span>:</a>
<a class="sourceLine" id="cb11-61" data-line-number="61">    </a>
<a class="sourceLine" id="cb11-62" data-line-number="62">    app <span class="op">=</span> QApplication(sys.argv)</a>
<a class="sourceLine" id="cb11-63" data-line-number="63">    ex <span class="op">=</span> Example()</a>
<a class="sourceLine" id="cb11-64" data-line-number="64">    sys.exit(app.exec_())</a></code></pre></div>
<p>本例中有一个菜单栏，一个置中的文本编辑框，一个状态栏。点击菜单栏选项会弹出一个<code>QtGui.QFileDialog</code>对话框，在这个对话框里，你能选择文件，然后文件的内容就会显示在文本编辑框里。</p>
<pre><code class="language-python">class Example(QMainWindow):

    def __init__(self):
        super().__init__()

        self.initUI()</code></pre>
<p>这里设置了一个文本编辑框，文本编辑框是基于<code>QMainWindow</code>组件的。</p>
<pre><code class="language-python">fname = QFileDialog.getOpenFileName(self, &#39;Open file&#39;, &#39;/home&#39;)</code></pre>
<p>弹出<code>QFileDialog</code>窗口。<code>getOpenFileName()</code>方法的第一个参数是说明文字，第二个参数是默认打开的文件夹路径。默认情况下显示所有类型的文件。</p>
<pre><code class="language-python">if fname[0]:
    f = open(fname[0], &#39;r&#39;)

    with f:
        data = f.read()
        self.textEdit.setText(data)</code></pre>
<p>读取选中的文件，并显示在文本编辑框内（但是打开HTML文件时，是渲染后的结果，汗）。</p>
<p>程序展示：</p>
<figure>
<img class="whitelist" src="docs/PyQt5/images/5-filedialog.png" alt="file Dialog" />
</figure>
